package com.kyl.service.impl;


import com.kyl.entity.RoomType;
import com.kyl.mapper.RoomTypeMapper;
import com.kyl.service.BedService;
import com.kyl.service.RoomService;
import com.kyl.service.RoomTypeService;
import com.kyl.service.UserService;
import com.kyl.utils.BeanConv;
import com.kyl.vo.RoomTypeVo;
import com.kyl.vo.RoomVo;
import com.kyl.vo.UserVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author WanJl
 * @version 1.0
 * @title RoomTypeServiceImpl
 * @description
 * @create 2025/1/18
 */
@Service
public class RoomTypeServiceImpl implements RoomTypeService {
    @Resource
    private RoomTypeMapper roomTypeMapper;
    @Resource
    private UserService userService;
    @Resource
    private RoomService roomService;
    @Resource
    private BedService bedService;
    @Override
    public List<RoomTypeVo> getRoomTypesList() {
        //查询出所有的房型信息
        List<RoomType> roomTypes = roomTypeMapper.getRoomTypesList();
        //把entity转换为VO，并且把每个房间类型的创建人查询出来放入VO对象

        List<RoomTypeVo> roomTypeVoList = roomTypes.stream().map(rt -> {
            //查询每个房型的创建人
            Long createBy = rt.getCreateBy();
            UserVo userVo = userService.getById(createBy);
            rt.setCreator(userVo.getRealName());
            //把entity转换为VO
            RoomTypeVo roomTypeVo = BeanConv.toBean(rt, RoomTypeVo.class);

            //查询每种房型的房间数量-根据房型名称查询房间数量
            List<RoomVo> roomVoList = roomService.getListByRoomTypeName(roomTypeVo.getName());
            roomTypeVo.setRoomCount(roomVoList.size());

            //查询每种房型的床位数量--根据房型名称查询床位数量
            int bedCount = (int) roomVoList.stream().filter(roomVo -> bedService.getBedsByRoomId(roomVo.getId()).size() > 0).count();
            roomTypeVo.setBedCount(bedCount);
            return roomTypeVo;
        }).collect(Collectors.toList());


        return roomTypeVoList;
    }
}
